#define _CRT_SECURE_NO_WARNINGS 1

#include<iostream>
#include<vector>
#include<deque>

using namespace std;

vector<int> maxSlidingWindow(vector<int>& nums, int k) {
    int n = nums.size();
    vector<int> res;
    deque<pair<int, int>> q;

    for (int i = 0; i < nums.size(); i++)
    {
        while (q.size() && q.front().second < i - k + 1) q.pop_front();
        while (q.size() && q.back().first < nums[i]) q.pop_back();
        q.push_back({ nums[i],i });
        if (i >= k - 1)
            res.push_back(q.front().first);
    }
    return res;
}

int largestRectangleArea(vector<int>& heights) {
    vector<pair<int, int>> stk;
    int res = 0;
    for (int i = 0; i < heights.size(); i++)
    {
        while (stk.size() && stk.back().first >= heights[i])
            stk.pop_back();
        stk.push_back({ heights[i],i });
        int pre = 0;
        for (int j = 0; j < stk.size(); j++)
        {
            int s = min(stk[j].first, heights[i]) * (i - pre + 1);
            res = max(res, s);
            pre = stk[j].second + 1;
        }
    }
    return res;
}